package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.dao.domain.CallLog;
import com.yunkeji.dao.mapper.CallLogMapper;
import com.yunkeji.dao.vo.CallLogVo;
import com.yunkeji.system.domain.bo.CallLogBo;
import com.yunkeji.system.service.ICallLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 通话日志Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class CallLogServiceImpl implements ICallLogService {

    private final CallLogMapper baseMapper;

    /** 查询通话日志 */
    @Override
    public CallLogVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询通话日志列表 */
    @Override
    public TableDataInfo<CallLogVo> queryPageList(CallLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CallLog> lqw = buildQueryWrapper(bo);
        Page<CallLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询通话日志列表 */
    @Override
    public List<CallLogVo> queryList(CallLogBo bo) {
        LambdaQueryWrapper<CallLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CallLog> buildQueryWrapper(CallLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CallLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, CallLog::getType, bo.getType());
        lqw.eq(bo.getCallerId() != null, CallLog::getCallerId, bo.getCallerId());
        lqw.eq(bo.getAnswerId() != null, CallLog::getAnswerId, bo.getAnswerId());
        lqw.eq(bo.getBelongUid() != null, CallLog::getBelongUid, bo.getBelongUid());
        lqw.eq(bo.getUcid() != null, CallLog::getUcid, bo.getUcid());
        lqw.orderByDesc(CallLog::getCreateTime);
        return lqw;
    }

    /** 新增通话日志 */
    @Override
    public Boolean insertByBo(CallLogBo bo) {
        CallLog add = BeanUtil.toBean(bo, CallLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改通话日志 */
    @Override
    public Boolean updateByBo(CallLogBo bo) {
        CallLog update = BeanUtil.toBean(bo, CallLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(CallLog entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除通话日志 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
